﻿using AutoMapper;
using Domain.Material_Management;
using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Material_Management;
using MES.Server.API.Application.Command.Production_Modules;
using MES.Server.API.Dto.Material_Management.OutPut;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Production_Modules
{
    public class GetMaterialsCommandHandler : IRequestHandler<GetMaterialsCommand, ApiResult<List<GetProductMaterialsOutPut>>>
    {
        private readonly IRepository<MaterialGroupModel> materialGroupRepo;
        private readonly IRepository<ProcessFlowModel> processFlowRepo;
        private readonly IRepository<ProductMaterialsModel> productMaterialsRepo;
        private readonly IMapper mapper;
        public GetMaterialsCommandHandler(IRepository<MaterialGroupModel> materialGroupRepo, IRepository<ProcessFlowModel> processFlowRepo, IMapper mapper, IRepository<ProductMaterialsModel> productMaterialsRepo)
        {
            this.materialGroupRepo = materialGroupRepo;
            this.processFlowRepo = processFlowRepo;
            this.mapper = mapper;
            this.productMaterialsRepo = productMaterialsRepo;
        }
        /// <summary>
        /// 产品物料查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<GetProductMaterialsOutPut>>> Handle(GetMaterialsCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<List<GetProductMaterialsOutPut>>();
            var productMaterials = productMaterialsRepo.GetAll().Where(x => x.IsDel == false);
            var materialGroup = materialGroupRepo.GetAll().Where(x => x.IsDel == false);
            var processFlow = processFlowRepo.GetAll().Where(x => x.IsDel == false);
            var linq = from a in productMaterials
                       join b in materialGroup on a.MaterialGroupId equals b.Id into ab
                       from b in ab.DefaultIfEmpty()
                       join c in processFlow on a.ProcessFlowId equals c.Id into ac
                       from c in ac.DefaultIfEmpty()
                       select new GetProductMaterialsOutPut
                       {
                           Id = a.Id,
                           ProductMaterialsCode = a.ProductMaterialsCode,
                           ProductMaterialsName = a.ProductMaterialsName,
                           ProductMaterialsType = a.ProductMaterialsType,
                           ProductMaterialsSpecs = a.ProductMaterialsSpecs,
                           ProductMaterialsUnit = a.ProductMaterialsUnit,
                           ProductMaterialsforewarn = a.ProductMaterialsforewarn,
                           ProductMaterialsDesc = a.ProductMaterialsDesc,
                           MaterialGroupId = b.Id,
                           MaterialGroupName = b.MaterialGroupName,
                           ProcessFlowId = c.Id,
                           FlowName = c.FlowName,
                           CreaterPeople = a.CreaterPeople,
                           CreateTime = a.CreateTime,
                           UpdateTime = a.UpdateTime ?? DateTime.Now,
                           ProductMaterialsCount = a.ProductMaterialsCount
                       };
            
                linq = linq.Where(x => x.MaterialGroupId == request.MaterialGroupId);

            result.Message = "查询成功";
            result.Code = StatusCode.Success;
            result.Data=linq.ToList();
            return Task.FromResult(result);
        }
    }
   
}
